# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/7/24

library(plyr)
library(gridExtra)
library(scales)
library(egg)
library(VennDiagram)
library(optparse)
library(magrittr)
library(tidyverse)

createWhenNoExist <- function(f){
    ! dir.exists(f) && dir.create(f)
}

option_list <- list(
make_option("--i", default = "AllMet.csv", type = "character", help = "metabolite data file"),
make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
make_option("--sc", default = "sample_color.txt", type = "character", help = "sample color file"),
make_option("--config", default = "config.csv", type = "character", help = "config file")
)

opt <- parse_args(OptionParser(option_list = option_list))

configData <- read.csv(opt$config, header = F, stringsAsFactors = F) %>%
    set_colnames(c("arg", "value")) %>%
    column_to_rownames("arg")

configData

borutaAfterTopN <- configData["borutaAfterTopN", "value"] %>%
  as.numeric()

sampleInfo <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
select(c("SampleID", "ClassNote"))

parent <- "./"
createWhenNoExist(parent)

originalDiffData <- read_csv("AllMet_Test.csv")

finalDf <- originalDiffData %>%
filter(IS_Sig==1)

sortDf <- originalDiffData %>%
  arrange(Uni_P)

borutaAfterDf <- sortDf %>%
  head(borutaAfterTopN)
write_tsv(borutaAfterDf, "boruta_after.txt")

outFileName <- paste0(parent, "/Markers.csv")
write.csv(finalDf, outFileName, row.names = F)





